//
//  ViewController.m
//  DocumentDemo
//
//  Created by 李晁宇 on 16/3/2.
//  Copyright © 2016年 李晁宇. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIDocumentInteractionControllerDelegate>

@end

@implementation ViewController {
    UIDocumentInteractionController *_docVC;
}

CGFloat getOriginX(CGFloat width) {
    return ([UIScreen mainScreen].bounds.size.width - width) / 2.0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SocialCalendar_protocol" ofType:@"docx"]];
    _docVC = [UIDocumentInteractionController interactionControllerWithURL:url];
    _docVC.delegate = self;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(getOriginX(100), 100, 100, 100)];
    label.text = @"请尝试轻拍";
    [self.view addSubview:label];
    
    UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:@[@"doc", @"excel", @"pdf", @"ppt", @"txt"]];
    control.frame = CGRectMake(getOriginX(300), 300, 300, 30);
    control.selectedSegmentIndex = 0;
    [control addTarget:self action:@selector(changeFormat:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:control];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_docVC presentPreviewAnimated:YES];
}

#pragma mark -  UISegmentedControl
- (void)changeFormat:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
        case 0: {
            NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SocialCalendar_protocol" ofType:@"docx"]];
            _docVC.URL = url;
            break;
        }
        case 1: {
            NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Workbook1" ofType:@"xlsx"]];
            _docVC.URL = url;
            break;
        }
        case 2: {
            NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"pdf"]];
            _docVC.URL = url;
            break;
        }
        case 3: {
            NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Swift 与 Auto Layout 最佳?实践" ofType:@"pptx"]];
            _docVC.URL = url;
            break;
        }
        case 4: {
            NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"笔试题答案" ofType:@"txt"]];
            _docVC.URL = url;
            break;
        }
        default:
            break;
    }
}

#pragma mark - UIDocumentInteractionControllerDelegate

// If preview is supported, this provides the view controller on which the preview will be presented.
// This method is required if preview is supported.
// If presenting atop a navigation stack, provide the navigation controller in order to animate in a manner consistent with the rest of the platform.
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller  {
    return self;
}

//// If preview is supported, these provide the view and rect that will be used as the starting point for the animation to the full screen preview.
//// The actual animation that is performed depends upon the platform and other factors.
//// If documentInteractionControllerRectForPreview is not implemented, the specified view's bounds will be used.
//// If documentInteractionControllerViewForPreview is not implemented, the preview controller will simply fade in instead of scaling up.
//- (nullable UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller {
//    return self.view;
//}
//
//- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller {
//    return self.view.frame;
//}
//
//// Preview presented/dismissed on document.  Use to set up any HI underneath.
//- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller {
//    self.view.backgroundColor = [UIColor orangeColor];
//}
//
//- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
//    self.view.backgroundColor = [UIColor whiteColor];
//}

@end
